package com.zp.qualitypractice.list;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;


/**
 * Created by izp on 2017/4/18.
 */

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {
    private Context mContext;
    private List<T> mList;
    private LayoutInflater mInflater;

    public BaseRecyclerViewAdapter(Context mContext, List<T> mList) {
        this.mContext = mContext;
        this.mList = mList;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(mInflater.inflate(getItemLayoutId(viewType),parent,false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        bindData(holder, position, mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public abstract int getItemLayoutId(int viewType);

    public abstract void bindData(BaseViewHolder holder, int position, T item);

    public void add(int positon, T item) {
        mList.add(positon, item);
        notifyItemInserted(positon);
    }

    public void delete(int positon) {
        mList.remove(positon);
        notifyItemRemoved(positon);
    }
}
